/*
 * jQuery scrollView
 * 此插件需配合handlebars一起使用
 * 后台返回的数据格式必需{data{page:{total:5}}这个层级
 */

(function ($) {
	function init(target){
		var opts = $.data(target,'scrollView').options;
		var url = getUrl(opts);
		
		getData(target,url,opts);
		
		var addHeight = $(target).height();
		var limitHeight = $(target).height() + $(target).offset().top - $(window).height();
		
		var total = opts.total;
		$(window).scroll(function(e,n){
			if($(target).css("display")=="none") return;
			if(opts.currentPage>=Math.ceil(total/opts.pernum)) return;
			var scrTop = $(this).scrollTop();
			if(scrTop>(limitHeight+addHeight*(opts.currentPage-1))){
				opts.currentPage++;
				//console.log(opts.currentPage);
				getData(target,pagerUrl(url,opts),opts);
			}
			
			
		});
		
	}
	//$(".firstList").children().length
	function getData(target,url,opts){
		$.ajax({
            url: url,
            type: opts.type,
            dataType: "json",
            async:false,
            success: function (data) {
                var template = Handlebars.compile(opts.tpl);
                var html = template(data);
                $(target).append(html);
                if($.data(target, 'scrollView').options.total==undefined){
                	if(data.data!=undefined){
                		$.data(target, 'scrollView').options.total = data.data.page.total;
                	}else{
                		$.data(target, 'scrollView').options.total = data.rows.page.total;
                	}
                }
            }
        });
	}
	
	function getUrl(opts){
		var url = opts.url;
		var filters = opts.filter; 
		var urlArr = urlSplit(url);
		
		var returnUrl = urlArr[0];
		
		for(var i=0,len=filters.length;i<len;i++){
			if(i==0){
				returnUrl = returnUrl + "?"+ filters[i] + "=" + $(this).find("input[name='"+filters[i]+"']").val();
			}else{
				returnUrl = returnUrl + "&"+ filters[i] + "=" + $(this).find("input[name='"+filters[i]+"']").val();
			}
		}
		if(filters.length==0){
			returnUrl = returnUrl + "?rows=" + opts.pernum;
		}else{
			returnUrl = returnUrl + "&rows=" + opts.pernum;
		}
		if(urlArr[1]!=""){
			if(urlArr[1].substring(0, 1)=="#"){
				returnUrl = returnUrl + urlArr[1];
			}else{
				returnUrl = returnUrl + "&" + urlArr[1];
			}
		}
		
		return returnUrl;
	}
	
	function pagerUrl(url,opts){
		var newUrl = url.split("#");
		if(newUrl[0].indexOf("?")){
			newUrl[0] = newUrl[0] + "&page=" +opts.currentPage;
		}else{
			if(newUrl[1]!=undefined) newUrl[0] = newUrl[0] + "?page=" +opts.currentPage+"#"+newUrl[1];
			else newUrl[0] = newUrl[0] + "?page=" +opts.currentPage;
		}
		
		if(newUrl[1]!=undefined) return newUrl[0]+"#"+newUrl[1];
		return newUrl[0];
	}
	
	function urlSplit(url){
		var url_1 = url.split("?");
		var args = "";
		if(url_1[1]!=undefined){
			args = url_1[1];
		}
		
		var url_2 = url_1[0].split("#");
		if(url_2[1]!=undefined){
			args =args + "#" + url_2[1];
		}
		return [url_2[0],args];
	}
	
	$.fn.scrollView = function(options, param){
		if (typeof options == 'string'){
			return $.fn.scrollView.methods[options](this, param);
		}
		
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'scrollView');
			if (state){
				$.extend(state.options, options);
			} else {
				$.data(this, 'scrollView', {
					options: $.extend({}, $.fn.scrollView.defaults, options),
				});
				
			}
			init(this);
		});
	};
	$.fn.scrollView.methods = {
	
	};
	
	
	$.fn.scrollView.defaults = {
		pernum:5,
		currentPage:1,
		filter:[],
		margin:0,
		url:"",
		tpl:null,
		type:"get"
	};
	
})(jQuery);